package com.lbt.educenter.controller;


import com.lbt.commonuntils.JwtUtils;
import com.lbt.commonuntils.R;
import com.lbt.commonuntils.ordervo.UcenterMemberOrder;
import com.lbt.educenter.entity.UcenterMember;
import com.lbt.educenter.entity.vo.RegisterVo;
import com.lbt.educenter.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-06-06
 */
@Api(description = "登录注册")
@RestController
//@CrossOrigin
@RequestMapping("/educenter/member")
public class UcenterMemberController {

    @Autowired
    UcenterMemberService ucenterMemberService;

    @Autowired
    RedisTemplate<String,String> redisTemplate;
    //登录
    @ApiOperation("登录")
    @PostMapping("/login")
    public R loginUser(@RequestBody UcenterMember member){

//        String o = redisTemplate.opsForValue().get(member.getMobile());
//        if (!StringUtils.isEmpty(o)&&o.length()==4){
//            return R.error().data("msg","操作频繁，请勿频繁操作");
//        }
        //jwt  生成token
    //    System.out.println("密码："+member.getPassword()+"\n"+"电话号码："+member.getMobile());
        String token=ucenterMemberService.login(member);
        return R.ok().data("token",token);
    }

    //注册
    @ApiOperation("注册")
    @PostMapping("/register")
    public R register(@RequestBody RegisterVo registerVo){
        System.out.println("注册方法执行了");
        ucenterMemberService.register(registerVo);
        return R.ok();
    }

    //根据token 获得用户信息
    @ApiOperation("根据token 获取用户信息")
    @GetMapping("/getMemberInfo")
    public R getUserInfo(HttpServletRequest request){
        //调用jwt工具类  获取id
        String id = JwtUtils.getMemberIdByJwtToken(request);
        //根据Id 获取用户信息
        System.out.println("根据id-------------------获取用户信息");
        UcenterMember member = ucenterMemberService.getById(id);
        return R.ok().data("userInfo",member);
    }

    //根据用户ID 获取用户信息 远程调用的时候  两方必须用同一对象
    //import com.lbt.commonuntils.ordervo.UcenterMemberOrder;
    @GetMapping("/getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable("id") String id){
        UcenterMember userInfo = ucenterMemberService.getById(id);
        UcenterMemberOrder ucenterMemberOrder=new UcenterMemberOrder();
        BeanUtils.copyProperties(userInfo,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    //查询注册人数
    @ApiOperation("查询注册人数")
    @GetMapping("/countRegister/{day}")
    public R countRegisterByDay(@PathVariable("day") String day){
        //自己创建一个方法  查询注册人数
       Integer count= ucenterMemberService.countRegisterDay(day);

        return R.ok().data("countRegister",count);
    }

}

